﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace jihuang_Player
{
    public partial class MainWindow : Form
    {
        public ChromiumWebBrowser chromeBrowser;
        public string htmlpath;
        public static string rootPath;
        public Thread thread;
        public static bool flag = false;
        public static bool debugmode = true;
        public MainWindow()
        {
            InitializeComponent();
            if (!debugmode)
            {
                button1.Hide();
                button2.Hide();
            }
            rootPath = System.IO.Directory.GetCurrentDirectory();
            htmlpath = @"file:///" + System.Web.HttpUtility.UrlEncode(rootPath.Replace(@"\", @"/")).Replace("%3a", ":").Replace("%2f", "/") + "/main.html";
            Console.WriteLine(htmlpath);
            CefSettings cSettings = new CefSettings();
            cSettings.MultiThreadedMessageLoop = true;
            cSettings.CefCommandLineArgs.Add("--disable-web-security", "");
            Cef.Initialize(cSettings);
            MessageBox.Show(htmlpath);
            chromeBrowser = new ChromiumWebBrowser(htmlpath);
            chromeBrowser.Dock = DockStyle.Fill;
            this.Controls.Add(chromeBrowser);
            chromeBrowser.FrameLoadEnd += (sender, args) =>
            {
                //Wait for the MainFrame to finish loading
                reflushProjetlist();
                if (args.Frame.IsMain)
                {
                    //args.Frame.ExecuteJavaScriptAsync(@" $.get('animation.xml', function (data) {buildan(data);})");
                }
            };
            chromeBrowser.JavascriptObjectRepository.Register("jsMethod", new jsMethod(this), false);

            //new PlayerForm("示例项目").Show();
            //new Compilation(@"E:\c#\jihuang_Player\bin\x64\Debug\Project\示例项目\").Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void reflushProjetlist()
        {
            DirectoryInfo root = new DirectoryInfo("Project\\");
            DirectoryInfo[] dics = root.GetDirectories();
            foreach (DirectoryInfo dir in dics)
            {
                chromeBrowser.ExecuteScriptAsync("setlist('" + dir.Name + "');");
            }
        }
        public void notifyreflushProjetlist()
        {
            chromeBrowser.ExecuteScriptAsync("reflushProjectList();");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            chromeBrowser.Load(htmlpath);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            chromeBrowser.ShowDevTools();
        }

        public void openProject(string name)
        {
            Thread t = new Thread(new ThreadStart(() =>
            {
                new PlayerForm(name).ShowDialog();
            }));
            t.Start();
        }

        public void importproject()
        {
            Thread thread = new Thread(new ThreadStart(() =>
              {
                  new loadxml(this).ShowDialog();
              }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        public void newProject()
        {
            flag = false;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                new newProject(this).ShowDialog();
            }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            Thread closeThread = new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    Thread.Sleep(100);
                    if (flag)
                    {
                        thread.Abort();
                        break;
                    }
                }
            }));
            closeThread.Start();
        }
        public void showinfo(string info)
        {
            chromeBrowser.ExecuteScriptAsync("showinfo('" + info + "');");
        }
    }

    public class jsMethod
    {
        private MainWindow mainWindow;
        public jsMethod(MainWindow m)
        {
            mainWindow = m;
        }
        public string getInfo()
        {
            return "张佳文";
        }

        public void reflushProjectList()
        {
            mainWindow.reflushProjetlist();
        }

        public void openProjet(string name)
        {
            mainWindow.openProject(name);
        }
        public void importproject()
        {
            mainWindow.importproject();
        }
        public void newProject()
        {
            mainWindow.newProject();
        }

        private void decodePublic(bool deffer)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择build.bin所在文件夹";
            Thread t = new Thread(new ThreadStart(() =>
            {
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string path = dialog.SelectedPath;
                    if (!File.Exists(path + @"\build.bin"))
                    {
                        MessageBox.Show("build.bin不存在");
                        return;
                    }
                    if (!File.Exists(path + @"\anim.bin"))
                    {
                        MessageBox.Show("anim.bin不存在");
                        return;
                    }
                    new Debin(mainWindow, path, deffer);
                }
            }));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }
        public void decode()
        {
            decodePublic(true);
        }

        public void decode2()
        {
            decodePublic(false);
        }
    }
}
